package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author stone
 * @date 2022/09/30 09:40
 */
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean {
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;

    public LifeCycleBean() {
        System.out.println("LifecycleBean的实例化");
    }

    String username;
    String beanName;

    public void setUsername(String username) {
        System.out.println("通过set方法给属性赋值");
        this.username = username;
    }

    @Override
    public void setBeanName(String beanName) {
        System.out.println("BeanNameAware的setBeanName方法");
        this.beanName = beanName;
    }

    public static final String NEED_BEAN_NAME = "songge";//组件的id或组件的name
    // 传入容器目的是为了啥 → 在当前组件中获得其他组件 → 尤其是使用一些框架的时候
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware的setBeanFactory方法");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware的setApplicationContext方法");
        this.applicationContext = applicationContext;
    }

    // 方法名不固定，自定义就可以了 → 需要做配置
    public void lifecycleInit() {
        System.out.println("自定义的init方法");
        // this.username = "雪茄";
    }

    // 昨天看SqlSessionFactoryBean的时候看过
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet方法");
    }

    // 销毁
    public void lifecycleDestroy() {
        System.out.println("自定义的destroy方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean接口的destroy方法");
    }
}
